package cn.tedu.submarine_1;


import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.Arrays;
import java.awt.Graphics;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;


public class World extends JPanel {
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;
    //窗口中的对象
    private Battleship ship=new Battleship();
    private SeaObject[] submrines={};
    private Mine[] mines={};
    private Bomb[] bombs={};

    //生成潜艇
    private SeaObject nextSubmarine(){
        Random rand=new Random();
        int type=rand.nextInt(20);
        if(type<10){
            return new ObserveSubmarine();
        }else if (type<16){
            return new TorpedoSubmarine();
        }else {
            return new MineSubmarine();
        }
    }
    private int subEnterIndex=0;
    private void submarineEnterAction(){
        subEnterIndex++;
        if(subEnterIndex%40==0){
            SeaObject obj=nextSubmarine();
            submrines=Arrays.copyOf(submrines,submrines.length+1);
            submrines[submrines.length-1]=obj;
        }

    }
    private int mineEnterIndex=0;//水雷进场计数
    private void mineEnterAction(){//每10毫秒走一次
        mineEnterIndex++;
        if(mineEnterIndex%100==0){
            for (int i = 0; i < submrines.length; i++) {//遍历所有的潜艇
                if (submrines[i] instanceof MineSubmarine){
                    MineSubmarine ms=(MineSubmarine) submrines[i];
                    Mine obj=ms.shootMine();
                    mines= Arrays.copyOf(mines,mines.length+1);
                    mines[mines.length-1]=obj;
                }
            }
        }
    }
    //海洋对象移动
    private void moveAction(){
        for (int i = 0; i < submrines.length; i++) {
            submrines[i].move();
        }
        for (int j = 0; j <mines.length ; j++) {
            mines[j].move();
        }
        for (int i = 0; i <bombs.length ; i++) {
            bombs[i].move();
        }
    }
    public void action(){
        Timer timer=new Timer();
        int interval=10;
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                submarineEnterAction();
                mineEnterAction();
                moveAction();
                repaint();
            }
        }, interval, interval);
    }
    public void paint(Graphics g){
        Images.sea.paintIcon(null,g,0,0);
        ship.painttImage(g);
        for (int i = 0; i <submrines.length ; i++) {
            submrines[i].painttImage(g);
        }
        for (int i = 0; i < mines.length; i++) {
            mines[i].painttImage(g);
        }
        for (int i = 0; i < bombs.length; i++) {
            bombs[i].painttImage(g);
        }
    }
    public static void main(String[] args) {
        JFrame frame = new JFrame(); //3.
        World world = new World(); //会创建窗口中的那一堆对象
        world.setFocusable(true);
        frame.add(world);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH+16, HEIGHT+39);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true); //系统自动调用paint()方法
        world.action(); //启动程序的执行
    }







}
